package com.itcast.cn.response;

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 响应字节数据
 *
 * @author hyatt
 * @version 1.0
 */
@WebServlet("/response3")
@SuppressWarnings("all")
public class ResponseTest3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\74220\\Pictures\\逍遥安卓照片\\172079.jpg");
        ServletOutputStream outputStream = response.getOutputStream();
        // 将文件写入输出流
//        byte[] bys = new byte[1024];
//        int lengh = 0;
//        while ((lengh = fileInputStream.read(bys)) != -1) {
//            outputStream.write(bys, 0, lengh);
//        }
        // 使用工具包 引入依赖 commons-io
        IOUtils.copy(fileInputStream, outputStream);
        fileInputStream.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
